package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.verify.MerchantApiPermission;
import com.whq.thrift.api.web.admin.service.verify.MerchantApiPermissionService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class MerchantApiPermissionController {

	private static final int DEFAULT_PAGE_SIZE = 25;
	
	@Autowired
	private MerchantApiPermissionService merchantApiPermissionService;
	
	@RequestMapping(value = "bs/merchantapipermission/list")
	public String listMerchantApiPermission(@RequestParam(value = "merchantName", required = false) String merchantName,
			@RequestParam(value = "apiMethod", required = false) String apiMethod,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {
		PageData<MerchantApiPermission> pageData = new PageData<MerchantApiPermission>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		// 查询参数
		if (StringUtil.isNotBlank(merchantName)) {
			whereParams.put("merchantName", merchantName);
		}
		if (StringUtil.isNotBlank(apiMethod)) {
			whereParams.put("apiMethod", apiMethod);
		}

		int totalCount = merchantApiPermissionService.getMerchantApiPermissionsCount(whereParams);
		pageData.setTotalCount(totalCount);

		int beginIndex = (currentPage - 1) * pageSize;
		
		whereParams.put("beginIndex", beginIndex);
		whereParams.put("maxCount", pageSize);
		List<MerchantApiPermission> resultList = merchantApiPermissionService.getMerchantApiPermissionList(whereParams);

		pageData.setResultList(resultList);

		model.addAttribute("merchantName", merchantName);
		model.addAttribute("apiMethod", apiMethod);
		model.addAttribute("pageData", pageData);
		return "bs/merchantapipermission/list";
	}
	
	@RequestMapping(value = "bs/merchantapipermission/show")
	public String showMerchantApiPermission(@RequestParam(value = "id", required = true) Integer id, Model model) {

		MerchantApiPermission merchantApiPermission = this.merchantApiPermissionService.getMerchantApiPermissionById(id);
		if (merchantApiPermission == null) {
			model.addAttribute("errMsg", "缺少参数: 找不到商户API权限!");
			return "error";
		}
		model.addAttribute("merchantApiPermission", merchantApiPermission);
		return "bs/merchantapipermission/show";
	}

	@RequestMapping(value = "bs/merchantapipermission/showAdd")
	public String showAddMerchantApiPermission(Model model) {
		MerchantApiPermission merchantApiPermission = new MerchantApiPermission();
		model.addAttribute("merchantApiPermission", merchantApiPermission);
		return "bs/merchantapipermission/showAdd";
	}

	@RequestMapping(value = "bs/merchantapipermission/add")
	public String addMerchantApiPermission(@Valid MerchantApiPermission merchantApiPermission, BindingResult result, Model model) {
		if (StringUtil.isBlank(merchantApiPermission.getMerchantName())) {
			model.addAttribute("errMsg", "商户名不能为空!");
			model.addAttribute("merchantApiPermission", merchantApiPermission);
			return "bs/merchantapipermission/showAdd";
		}
		
		if (StringUtil.isBlank(merchantApiPermission.getApiMethod())) {
			model.addAttribute("errMsg", "API方法不能为空!");
			model.addAttribute("merchantApiPermission", merchantApiPermission);
			return "bs/merchantapipermission/showAdd";
		}

		this.merchantApiPermissionService.addMerchantApiPermission(merchantApiPermission);
		return "redirect:show.do?id=" + merchantApiPermission.getId();
	}

	@RequestMapping(value = "bs/merchantapipermission/showUpd")
	public String showUpd(@RequestParam(value = "id", required = true) int id, Model model) {
		if (id <= 0) {
			model.addAttribute("errMsg", "缺少参数: id!");
			return "error";
		}
		MerchantApiPermission merchantApiPermission = this.merchantApiPermissionService.getMerchantApiPermissionById(id);
		if (merchantApiPermission == null) {
			model.addAttribute("errMsg", "该商户API权限不存在!");
			return "error";
		}
		model.addAttribute("merchantApiPermission", merchantApiPermission);
		return "bs/merchantapipermission/showUpd";
	}

	@RequestMapping(value = "bs/merchantapipermission/upd")
	public String upd(@Valid MerchantApiPermission merchantApiPermission, BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("merchantApiPermission", merchantApiPermission);
			return "bs/merchantapipermission/showUpd";
		}
		
		if(StringUtil.isBlank(merchantApiPermission.getMerchantName())){
			model.addAttribute("errMsg", "商户名不能为空！");
			model.addAttribute("merchantApiPermission", merchantApiPermission);
			return "bs/merchantapipermission/showUpd";
		}
		
		if(StringUtil.isBlank(merchantApiPermission.getApiMethod())){
			model.addAttribute("errMsg", "API方法不能为空！");
			model.addAttribute("merchantApiPermission", merchantApiPermission);
			return "bs/merchantapipermission/showUpd";
		}

//		MerchantApiPermission merchantByName = merchantApiPermissionService.getMerchantApiPermissionByName(merchantApiPermission.getMerchantName());
//		if (merchantByName != null){
//			model.addAttribute("errMsg", "商户名已存在！");
//			model.addAttribute("merchantApiPermission", merchantApiPermission);
//			return "bs/merchantapipermission/showUpd";
//		}
//		
//		MerchantApiPermission merchantByApi = merchantApiPermissionService.getMerchantApiPermissionByApi(merchantApiPermission.getApiMethod());
//		if (merchantByApi != null){
//			model.addAttribute("errMsg", "Api方法已存在！");
//			model.addAttribute("merchantApiPermission", merchantApiPermission);
//			return "bs/merchantapipermission/showUpd";
//		}

		this.merchantApiPermissionService.updMerchantApiPermission(merchantApiPermission);
		return "redirect:show.do?id=" + merchantApiPermissionService.getMerchantApiPermissionByName(merchantApiPermission.getMerchantName()).getId();
	}

	@RequestMapping(value = "bs/merchantapipermission/del")
	public String del(@RequestParam(value = "id", required = true, defaultValue = "0") Integer id, Model model) {
		this.merchantApiPermissionService.delMerchantApiPermission(id);
		return "bs/merchantapipermission/delOk";
	}

	@RequestMapping(value = "bs/merchantapipermission/dels")
	public String dels(int[] delIds, Model model) {
		if (delIds != null) {
			for (int i = 0; i < delIds.length; i++) {
				this.merchantApiPermissionService.delMerchantApiPermission(delIds[i]);
			}
		}
		return "redirect:list.do";
	}
}
